package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.local.DeviceAccessoryMapper;
import com.zhijian.medical.entity.po.DeviceAccessory;
import com.zhijian.medical.entity.vo.request.DeviceAccessorySearchVo;
import com.zhijian.medical.entity.vo.response.DeviceAccessoryVo;
import com.zhijian.medical.service.DeviceAccessoryService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 仪器配件业务处理层
 *
 * @author HCG
 * @date 2022-01-01 15:37:54
 * @version: 1.0
 */
@Slf4j
@Service
public class DeviceAccessoryServiceImpl implements DeviceAccessoryService {

    @Autowired
    private DeviceAccessoryMapper mapper;

    @Override
    public DeviceAccessory selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public DeviceAccessoryVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(DeviceAccessory deviceAccessory) {
        Date now = new Date();
        deviceAccessory.setCreateTime(now);
        deviceAccessory.setUpdateTime(now);
        return mapper.insert(deviceAccessory) == 1 ? deviceAccessory.getId() : 0L;
    }

    @Override
    public boolean update(DeviceAccessory deviceAccessory) {
        Date now = new Date();
        deviceAccessory.setUpdateTime(now);
        return mapper.updateByPrimaryKey(deviceAccessory) != 0;
    }

    @Override
    public Integer count(DeviceAccessorySearchVo deviceAccessorySearchVo) {
        return mapper.count(deviceAccessorySearchVo);
    }

    @Override
    public List<DeviceAccessoryVo> selectList(DeviceAccessorySearchVo deviceAccessorySearchVo) {
        return mapper.selectList(deviceAccessorySearchVo);
    }

    @Override
    public Page<DeviceAccessoryVo> selectPage(DeviceAccessorySearchVo deviceAccessorySearchVo) {
        //先直接查总数
        int count = count(deviceAccessorySearchVo);
        //分页
        Page<DeviceAccessoryVo> page = new Page<>(true, count, deviceAccessorySearchVo.getPageNum(), deviceAccessorySearchVo.getPageSize());
        deviceAccessorySearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<DeviceAccessoryVo> responseVos = this.selectList(deviceAccessorySearchVo);
        page.setData(responseVos);
        return page;
    }

}